package cc.comac.mom.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import cc.comac.mom.api.EquipDevService;
import cc.comac.mom.bean.EquipDev;
import cc.comac.mom.bean.EquipDevBean;
import cc.comac.mom.util.ExcelUploadUtil;
import cc.comac.mom.util.ExportExcel;
import cc.comac.mom.util.ResultBean;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(value ="/EquipDev")
public class EquipDevController {
	@Autowired
	private EquipDevService equipDevService;

	@RequestMapping(value = "/save",method = RequestMethod.POST)
	public ResultBean save(@RequestBody EquipDev obj) {
		
		return equipDevService.save(obj);
		
	}
	
	@RequestMapping(value = "/update",method = RequestMethod.POST)
	public ResultBean update(@RequestBody EquipDev obj) {
		
		return equipDevService.update(obj);
		
	}
	
	@RequestMapping(value = "/delete",method = RequestMethod.POST)
	public ResultBean delete(@RequestBody EquipDev obj) {
		
		return equipDevService.delete(obj);
		
	}
	
	@RequestMapping(value = "/getById",method = RequestMethod.POST)
	public EquipDev getById(@RequestBody EquipDev obj) {
		return equipDevService.getById(obj);
		
	}
	
	@RequestMapping(value = "/selectByCondition",method = RequestMethod.POST)
	public ResultBean selectByCondition(@RequestBody EquipDev obj) {
		return equipDevService.selectByCondition(obj);
	}

	@RequestMapping(value = "/select",method = RequestMethod.POST)
	public ResultBean select(@RequestBody EquipDevBean obj) {
		return equipDevService.select(obj);
	}

	
	@RequestMapping(value = "/selectAllData",method = RequestMethod.POST)
	public ResultBean selectAllData () {
		ResultBean selectAllData = equipDevService.selectAllData();
		return selectAllData;
	}
	@ApiOperation(value = "设备主数据导入模板下载")
	@RequestMapping(value = "/TemplateDownload")
	public @ResponseBody void TemplateDownload(HttpServletResponse request, HttpServletResponse response)
			throws Exception {

		String fileName = "设备主数据" + "-" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".xls";
		ArrayList<String> listName = new ArrayList<String>();
		
		listName.add("设备编号(唯一性)");
		listName.add("设备名称");		
		listName.add("规格型号");
		listName.add("设备类型");
		listName.add("计量单位");
		listName.add("品牌商");
		listName.add("资产类别");		
		listName.add("资产状态");
		listName.add("入账日期");
		listName.add("备注");
		listName.add("设备状态");
		listName.add("使用部门");
		listName.add("所在位置");
		listName.add("具体位置");
		listName.add("维修组");
		listName.add("使用部门班组");
		listName.add("使用部门领导");
		listName.add("使用责任人");
		listName.add("是否考核(1:考核，0：不考核)");
		listName.add("考核周期(如：2020-07-20 10:30:00~2020-07-21 10:30:00)");
		listName.add("完好状态(1:是，0:否)");
		listName.add("完好有效期");
		listName.add("盘点日期");
		ExcelUploadUtil.TemplateDownload(request, response, fileName, listName);
	//	equipDevService.templateDownload(request, response);
	}
	
	
	@ApiOperation(value = "设备主数据导入数据")
	@RequestMapping(value = "/TemplateDataUpload", method = RequestMethod.POST)
	public @ResponseBody ResultBean dataUpload(@RequestPart("file") MultipartFile file,
			HttpServletRequest request) throws IOException {
		// 文件所在的临时文件夹
		String rootPath = request.getServletContext().getRealPath("/") + "uploadExcel";
			ResultBean resultBean2 = equipDevService.importEquipDev(file,rootPath);
			return resultBean2;
	}
	
	
	@ApiOperation(value = "设备主数据导出数据")
	@RequestMapping(value = "/toExportExcel",method = RequestMethod.POST)  
	public void exportExcel(HttpServletRequest request, HttpServletResponse response,@RequestBody EquipDevBean obj) {
		String filePath = equipDevService.exportData(request, response, obj);
		ExportExcel.download(filePath, response);
		ExportExcel.createDir(filePath);
	}
}
